package com.samapp.indent;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.samapp.MyApplication;
import com.samapp.R;
import com.samapp.base.BaseActivity;
import com.samapp.entity.DetectionItem;

public class DetectionItemListActivity extends BaseActivity implements OnClickListener {

	/** 返回按钮 */
	private Button btnBack = null;
	private PullToRefreshListView listViewDetectionItemList = null;
	private DetectionItemListAdapter mDetectionItemListAdapter;
	private MyApplication myApplication = MyApplication.getApplicationInstance();
	
	/** 是否加载完所有记录 */
	private boolean isLoadAllRecord = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detection_item_list);
		init();
	}

	private void init() {
		initView();
		initListView();
		initListener();
	}

	private void initView() {
		btnBack = (Button) findViewById(R.id.btn_back);
		listViewDetectionItemList = (PullToRefreshListView) findViewById(R.id.listview_detection_item_list);
	}

	private void initListView() {
		listViewDetectionItemList.setMode(PullToRefreshListView.Mode.BOTH);
		// 上拉
		listViewDetectionItemList.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载更多");
		listViewDetectionItemList.getLoadingLayoutProxy(false, true).setReleaseLabel("放开加载");
		listViewDetectionItemList.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
		// 下拉
		listViewDetectionItemList.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新");
		listViewDetectionItemList.getLoadingLayoutProxy(true, false).setReleaseLabel("放开加载");
		listViewDetectionItemList.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载...");
		
		mDetectionItemListAdapter = new DetectionItemListAdapter(DetectionItemListActivity.this, myApplication.listDetectionItem);
		listViewDetectionItemList.setAdapter(mDetectionItemListAdapter);		
	}

	private void initListener() {
		btnBack.setOnClickListener(this);
		
		listViewDetectionItemList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				DetectionItem detectionItem = myApplication.listDetectionItem.get(position-1);
				if(detectionItem != null){
					myApplication.curDetetionFid = detectionItem.getId();
					skip(ViewTestMethodActivity.class);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			myApplication.listDetectionItem.clear();
			finish();
			break;
		}
	}
	
	/** 跳转页面 */
	private void skip(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
		finish();
	}
	
	private void onRefreshComplete() {
		if (isLoadAllRecord) {
			showToast("加载完全部数据！");
		}
		mDetectionItemListAdapter.notifyDataSetChanged();
		listViewDetectionItemList.onRefreshComplete();
	}
}
